package com.example.mypra1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mypra1.adapter.MyFragmentPagerAdapter;
import com.example.mypra1.fragments.MainFragment1;
import com.example.mypra1.fragments.MainFragment2;
import com.example.mypra1.fragments.MainFragment3;
import com.example.mypra1.fragments.MainFragment4;
import com.example.mypra1.fragments.MoreFragment1;
import com.example.mypra1.fragments.MoreFragment2;
import com.example.mypra1.fragments.MoreFragment3;
import com.example.mypra1.fragments.MoreFragment4;
import com.example.mypra1.fragments.MoreFragment5;

import java.util.ArrayList;

/**
 * 更多
 */
public class MoreActivity extends AppCompatActivity implements View.OnClickListener{
    private ArrayList<Fragment> fragments;
    ViewPager2 viewPager;
    LinearLayout tab1, tab2, tab3, tab4,tab5,tCurrent;
    TextView tvTab1, tvTab2, tvTab3, tvTab4,tvTab5,tvCurrent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        initPager();
        initView();
        setListener();
    }
    //同app主页面，点击导航栏标签，实现碎片切换
    private void initView() {
        tab1 = findViewById(R.id.more_tab1);
        tab2 = findViewById(R.id.more_tab2);
        tab3 = findViewById(R.id.more_tab3);
        tab4 = findViewById(R.id.more_tab4);
        tab5 = findViewById(R.id.more_tab5);
        tvTab1 = findViewById(R.id.mt_tab1);
        tvTab2 = findViewById(R.id.mt_tab2);
        tvTab3 = findViewById(R.id.mt_tab3);
        tvTab4 = findViewById(R.id.mt_tab4);
        tvTab5 = findViewById(R.id.mt_tab5);
        tvTab1.setSelected(true);
        tab1.setSelected(true);
        tCurrent = tab1;
        tvCurrent = tvTab1;
    }
    private void initPager() {
        viewPager = findViewById(R.id.more_vp);
        viewPager.setUserInputEnabled(false);
        fragments = new ArrayList<>();
        fragments.add(MoreFragment1.newInstance());
        fragments.add(MoreFragment2.newInstance());
        fragments.add(MoreFragment3.newInstance());
        fragments.add(MoreFragment4.newInstance());
        fragments.add(MoreFragment5.newInstance());
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0,false);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }
        });
    }
    private void changeTab(int position) {
        tvCurrent.setSelected(false);
        tCurrent.setSelected(false);
        switch (position) {
            case R.id.more_tab1:
                viewPager.setCurrentItem(0, false);
            case 0:
                tvTab1.setSelected(true);
                tab1.setSelected(true);
                tCurrent = tab1;
                tvCurrent = tvTab1;
                break;
            case R.id.more_tab2:
                viewPager.setCurrentItem(1, false);
            case 1:
                tvTab2.setSelected(true);
                tab2.setSelected(true);
                tCurrent = tab2;
                tvCurrent = tvTab2;
                break;
            case R.id.more_tab3:
                viewPager.setCurrentItem(2, false);
            case 2:
                tvTab3.setSelected(true);
                tab3.setSelected(true);
                tCurrent = tab3;
                tvCurrent = tvTab3;
                break;
            case R.id.more_tab4:
                viewPager.setCurrentItem(3, false);
            case 3:
                tvTab4.setSelected(true);
                tab4.setSelected(true);
                tCurrent = tab4;
                tvCurrent = tvTab4;
                break;
            case R.id.more_tab5:
                viewPager.setCurrentItem(4,false);
            case 4:
                tvTab5.setSelected(true);
                tab5.setSelected(true);
                tCurrent = tab5;
                tvCurrent = tvTab5;
                break;
        }
    }
    private void setListener() {
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);
        tab5.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        changeTab(view.getId());
    }
}